package com.hqd.ch03.v48.factory.support;

import com.hqd.ch03.utils.ObjectUtils;
import com.hqd.ch03.v48.beans.factory.config.BeanDefinition;
import com.hqd.ch03.v48.beans.factory.support.AbstractBeanDefinition;

/**
 * 既可以独立使用，也可以通过设置 parentName 属性来实现继承关系，灵活性更高
 */
public class GenericBeanDefinition extends AbstractBeanDefinition {

    /**
     * 父类bean
     */
    private String parentName;

    public GenericBeanDefinition() {
        super();
    }

    public GenericBeanDefinition(BeanDefinition original) {
        super(original);
    }

    public GenericBeanDefinition(String beanClassName) {
        setBeanClassName(beanClassName);
    }

    public GenericBeanDefinition(Class beanClass) {
        setBeanClassName(beanClass.getName());
    }

    @Override
    public String getParentName() {
        return this.parentName;
    }

    @Override
    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    @Override
    public AbstractBeanDefinition cloneBeanDefinition() {
        return new GenericBeanDefinition(this);
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof GenericBeanDefinition)) {
            return false;
        }
        GenericBeanDefinition that = (GenericBeanDefinition) other;
        return (ObjectUtils.nullSafeEquals(this.parentName, that.parentName) && super.equals(other));
    }

    @Override
    public String toString() {
        if (this.parentName != null) {
            return "Generic bean with parent '" + this.parentName + "': " + super.toString();
        }
        return "Generic bean: " + super.toString();
    }

}
